package com.jmp.sms;

import one.stand.util.AssertUtil;
import one.stand.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {
    @Autowired
    private SmsProperties smsProperties;
    @Autowired
    private RedisUtil redisUtil;

    @Bean
    public SmsClient smsClient() {
        AssertUtil.nonNull(smsProperties.getClientList(), "sms config must not be null");
        ComplexSmsClient smsClient = new ComplexSmsClient();
        for (SmsProperties.OneSms oneSms : smsProperties.getClientList()) {
            smsClient.addClient(new DefaultSmsClient(smsProperties.getTemplateCode(), oneSms));
        }
        return smsClient;
    }

    @Bean
    public CaptchaService captchaService() {
        return new CaptchaServiceImpl(redisUtil, smsClient());
    }
}
